/*
	业务中心应用路由定义
*/
(function() {
	var Business = APM.Business = createApp("business");

	function heightScroll() {
		setTimeout(function() {
			$(".application-main").css("overflow-y", "scroll");
			$(".application-main").height($(window).height() - 49);
			$(window).on("resize." + this.elementId, function() {
				$(".application-main").height($(window).height() - 49);
			});
		}, 10)
	}

	//路由器配置
	Business.routerMap(function() {
		//业务总览
		this.route("overview");
		//业务中心
		this.route("center");
		//业务详情
		this.route("detail", { path: "/:app_id" }, function() {
			//业务仪表板
			this.route("gauge");
			//业务交易
			this.route("trades", function() {
				//关键交易
				this.route("key");
				//所有交易
				this.route("all");
			});
			//最终用户体验
			this.route("experience", function() {
				//体验概况
				this.route("default");
				//浏览器
				this.route("browser");
				//页面
				this.route("page");
				//位置
				this.route("area");
			});
			//节点详情
			this.route("nodedetail", { path: "/:node_id" }, function() {
				//服务概况
				this.route("overview");
				//交易类型
				this.route("type");
				//交易渠道
				this.route("channel");
				//多维统计
				this.route("multidimensional");
				//错误码
				this.route("errorcode");
				//最新告警
				this.route("alarms");
				//错误交易
				this.route("wrongtransaction");
				//慢交易
				this.route("slowtrade");
				//集群节点
				this.route("cluster");
				//交易跟踪
				this.route("track");
				//动态菜单
				this.route("dynamic", {
					path: "/:menu_id"
				});
			});
			//交易类型详情
			this.route("nodetype", { path: "/nodetype/:nodetype_id" }, function() {
				//交易概况
				this.route("overviewtype");
				//最新告警
				this.route("alarms");
				//错误码
				this.route("errorcode");
			});
			//交易详情
			this.route("trade", { path: "/trade/:trade_id" }, function() {
				//交易概况
				this.route("overview");
				//错误码
				this.route("errorcode");
				//最新告警
				this.route("alarms");
				//交易跟踪
				this.route("track");
			});
			//配置管理
			this.route("config");
		});
	});

	APM.BusinessRoute = Business.Route.extend({
		templates: [{
			name: "main/frame",
			url: "/apps/main/tpls/main_frame.hbs"
		}],

		dependences: [
			"/apps/main/ctrls/frame_ctrl",
			"/apps/main/views/frame_view"
		],

		renderTemplate: function() {
			this.render("main/frame", {
				controller: "mainFrame",
				view: "mainFrame"
			});
		}
	});

	//进入应用过后跳转到应用默认访问的页面
	APM.BusinessIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.center");
		}
	});

	/*---------路由定义----------- */
	//业务总览
	APM.BusinessOverviewRoute = Business.Route.extend({
		dependences: [
			"./ctrls/overview_ctrl",
			"./views/overview_view",
			"./ctrls/center_ctrl",
			"./ctrls/detail_ctrl"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.startRealTimeMode();
			ctrl.overviewPageSystem();
			overviewOrCenterSwitch = "business";
			$.cookie("isThreshold", "business", '/');
			ctrl.set("isTradingMode", true);
			heightScroll();
			ctrl.clickSetButton();
		},
		deactivate: function() {
			this.controller.reset();
			this.set("isTradingMode", false);
			this.controller.stopRealTimeMode();
			$.cookie("isThreshold", "", -1);
		}
	});

	//业务中心
	APM.BusinessCenterRoute = Business.Route.extend({
		dependences: [
			"./ctrls/center_ctrl",
			"./views/center_view"
		],

		model: function() {
			return this.store.ajax("GET", "applications")
				.then(function(response) {
					var applications = Ember.A(response.applications);
					applications.forEach(function(app) {
						app.viewName = "appSequenceView_" + app.id;
					});
					return applications;
				});
		},

		setupController: function(ctrl, model, transition) {
			ctrl.set("model", model);
			ctrl.startRealTimeMode();
			$.cookie("isThreshold", "system", '/');
			overviewOrCenterSwitch = "system";
			heightScroll();
		},

		deactivate: function() {
			this.controller.stopRealTimeMode();
			$.cookie("isThreshold", "", -1);
			$(".application-main").css("overflow-y", "hidden");
		}
	});

	//业务详情
	APM.BusinessDetailRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_ctrl",
			"./ctrls/center_ctrl",
			"./views/detail_view"
		],
		queryParams: {
			startTime: {
				refreshModel: true
			},
			pinTimePoint: {
				refreshModel: true
			}
		},
		model: function(params, transition) {
			var route = this;
			if ($.cookie("isThreshold")) {
				var cookieNum = $.cookie("isThreshold");
				if (cookieNum == "system") {
					overviewOrCenterSwitch = "system";
				} else {
					overviewOrCenterSwitch = "business";
				}
			}

			return this.store.ajax("GET", "applications/" + params.app_id, {
					"panelId": overviewOrCenterSwitch
				})
				.then(function(response) {
					var app = response.application;
					Ember.A(app.topoNodes);
					Ember.A(app.relations);
					return app;
				}, function() {
					transition.abort();
					alertTip("没找到对应的业务系统!");
					route.transitionTo("business.center");
				});
		},
		setupController: function(ctrl, model, transition) {
			ctrl.set("model", model);
			ctrl.setup(transition.queryParams.startTime, transition.queryParams.pinTimePoint);
			heightScroll();
		},
		deactivate: function() {
			this.controller.reset();
			this.controller.stopRealTimeMode();
		}
	});

	//业务详情索引界面默认跳转到业务仪表板
	APM.BusinessDetailIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.gauge");
		}
	});

	//业务仪表板
	APM.BusinessDetailGaugeRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_gauge_ctrl",
			"./views/detail_gauge_view"
		],
		setupController: function(ctrl, params, transition) {
			var business = ctrl.get("business"),
				isWhere = ctrl.get("isWhere");

			ctrl.addObserver4AppPerformances();

			if (business) {
				ctrl.businessNodesChange();
			}

			if (!ctrl.get("cancelSet")) {
				if (overviewOrCenterSwitch == "business") {
					ctrl.set("isShowSystemOrBusiness", false);
				} else {
					ctrl.set("isShowSystemOrBusiness", true);
				}
			}
		},
		deactivate: function() {
			this.controller.reset();
			this.controller.set("cancelSet", false);
		}
	});

	//业务交易
	APM.BusinessDetailTradesRoute = Business.Route.extend();

	//业务交易索引界面默认跳转到关键交易
	APM.BusinessDetailTradesIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.trades.key");
		}
	});

	//关键交易
	APM.BusinessDetailTradesKeyRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_trades_key_ctrl",
			"./views/detail_trades_key_view",
			"./ctrls/center_ctrl"
		]
	});

	//所有交易
	APM.BusinessDetailTradesAllRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_trades_all_ctrl",
			"./views/detail_trades_all_view"
		]
	});

	//交易详情
	APM.BusinessDetailTradeRoute = Business.Route.extend({
		model: function(params, transition) {
			var route = this;
			return this.store.ajax("GET", "keytrades/" + params.trade_id)
				.then(function(response) {
					return response.result;
				}, function() {
					transition.abort();
					alertTip("没找到对应的关键交易!");
					route.transitionTo("business.detail.trades");
				});
		}
	});

	APM.BusinessDetailTradeIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.trade.overview");
		}
	});

	//交易详情-交易概况
	APM.BusinessDetailTradeOverviewRoute = Business.Route.extend({
		templates: [{
			name: "business/detail/nodedetail/overview",
			url: "/apps/business/tpls/business_detail_nodedetail_overview.hbs"
		}],

		dependences: [
			"./ctrls/detail_trade_overview_ctrl",
			"./views/detail_nodedetail_overview_view"
		],

		setupController: function(ctrl, params, transition) {
			ctrl.setup(params);
			heightScroll();
		},

		renderTemplate: function() {
			this.render("business/detail/nodedetail/overview", {
				controller: "businessDetailTradeOverview",
				view: "businessDetailNodedetailOverview"
			});
		}
	});

	//交易详情-错误码
	APM.BusinessDetailTradeErrorcodeRoute = Business.Route.extend({
		templates: [{
			name: "business/detail/nodedetail/errorcode",
			url: "/apps/business/tpls/business_detail_nodedetail_errorcode.hbs"
		}],

		dependences: [
			"./ctrls/detail_trade_errorcode_ctrl",
			"./views/detail_nodedetail_errorcode_view"
		],
		renderTemplate: function() {
			this.render("business/detail/nodedetail/errorcode", {
				controller: "businessDetailTradeErrorcode",
				view: "businessDetailNodedetailErrorcode"
			});
		},
		setupController: function(ctrl, params, transition) {
			ctrl.initTime();
			heightScroll();
		}
	});

	//节点详情-交易类型
	APM.BusinessDetailNodedetailTypeRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_type_ctrl",
			"./views/detail_nodedetail_type_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", params);
			ctrl.initTime();
			ctrl.requireData();
		},

	});

	//节点详情-交易渠道
	APM.BusinessDetailNodedetailChannelRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_channel_ctrl",
			"./views/detail_nodedetail_channel_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", params);
			ctrl.initTime();
			ctrl.requireData();
		},
	});

	//节点详情-多维统计
	APM.BusinessDetailNodedetailMultidimensionalRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_multidimensional_ctrl",
			"./views/detail_nodedetail_multidimensional_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.initTime();
			heightScroll();
			ctrl.set("model", model);
		},
		deactivate: function() {
			this.controller.reset();
		}
	});

	//交易详情-交易跟踪
	APM.BusinessDetailTradeTrackRoute = Business.Route.extend({
		templates: [{
			name: "business/detail/nodedetail/track",
			url: "/apps/business/tpls/business_detail_nodedetail_track.hbs"
		}],

		dependences: [
			"./ctrls/detail_trade_track_ctrl",
			"./views/detail_nodedetail_track_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.setup(params);
		},

		renderTemplate: function() {
			this.render("business/detail/nodedetail/track", {
				controller: "businessDetailTradeTrack",
				view: "businessDetailNodedetailTrack"
			});
		}
	});

	//交易详情-最新告警
	APM.BusinessDetailTradeAlarmsRoute = Business.Route.extend({
		templates: [{
			name: "business/detail/nodedetail/alarms",
			url: "/apps/business/tpls/business_detail_nodedetail_alarms.hbs"
		}],

		dependences: [
			"./ctrls/detail_trade_alarms_ctrl",
			"./views/detail_nodedetail_alarms_view"
		],

		renderTemplate: function() {
			this.render("business/detail/nodedetail/alarms", {
				controller: "businessDetailTradeAlarms",
				view: "businessDetailNodedetailAlarms"
			});
		}
	});

	//交易跟踪
	APM.BusinessDetailTradeTrackRoute = Business.Route.extend({
		templates: [{
			name: "business/detail/nodedetail/track",
			url: "/apps/business/tpls/business_detail_nodedetail_track.hbs"
		}],

		dependences: [
			"./ctrls/detail_trade_track_ctrl",
			"./views/detail_nodedetail_track_view"
		],
		renderTemplate: function() {
			this.render("business/detail/nodedetail/track", {
				controller: "businessDetailTradeTrack",
				view: "businessDetailNodedetailTrack"
			});
		}
	});

	//交易类型详情
	APM.BusinessDetailNodetypeRoute = Business.Route.extend({
		model: function(params, transition) {
			var route = this;

			return this.store.ajax("GET", "keytrades/" + params.nodetype_id)
				.then(function(response) {
					return response.result;
				}, function() {
					transition.abort();
					alertTip("没找到对应的关键交易!");
					route.transitionTo("business.detail.trades");
				});
		}
	});
	APM.BusinessDetailNodetypeIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.nodetype.overviewtype");
		}
	});


	//交易类型详情-服务概况 
	APM.BusinessDetailNodetypeOverviewtypeRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodetype_overviewtype_ctrl",
			"./views/detail_nodetype_overviewtype_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.set("model", params);
			ctrl.setup(params)
		}
	});

	//交易类型详情-错误 
	APM.BusinessDetailNodetypeErrorcodeRoute = Business.Route.extend({

/*		templates: [{
			name: "business/detail/nodedetail/errorcode",
			url: "/apps/business/tpls/business_detail_nodedetail_errorcode.hbs"
		}],*/

		dependences: [
			"./ctrls/detail_nodetype_errorcode_ctrl",
			"./views/detail_nodetype_errorcode_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", params);
			ctrl.initTime();
			ctrl.requireData();
		}
/*		renderTemplate: function() {
			this.render("business/detail/nodedetail/errorcode", {
				controller: "businessDetailNodetypeErrorcode",
				view: "businessDetailNodedetailErrorcode"
			});
		}*/
	});


	//交易类型详情-告警 
	APM.BusinessDetailNodetypeAlarmsRoute = Business.Route.extend({
/*		templates: [{
			name: "business/detail/nodedetail/alarms",
			url: "/apps/business/tpls/business_detail_nodedetail_alarms.hbs"
		}],*/
		dependences: [
			"./ctrls/detail_nodetype_alarms_ctrl",
			"./views/detail_nodetype_alarms_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", params);
			ctrl.initTime();
			ctrl.requireAlerts();
		}
/*		renderTemplate: function() {
			this.render("business/detail/nodedetail/alarms", {
				controller: "businessDetailNodetypeAlarms",
				view: "businessDetailNodedetailAlarms"
			});
		}*/
	});

	//最终用户体验
	APM.BusinessDetailExperienceRoute = Business.Route.extend();

	APM.BusinessDetailExperienceIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.experience.default");
		}
	});

	//最终用户体验-体验概况
	APM.BusinessDetailExperienceDefaultRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_experience_default_ctrl",
			"./views/detail_experience_default_view"
		],
		setupController: function(ctrl, params, transition) {
			if (overviewOrCenterSwitch == "business") {
				ctrl.set("panelTitle", "交易量");
			} else {
				ctrl.set("panelTitle", "吞吐量");
			}
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("cancelSet", false);
		}
	});

	//最终用户体验-浏览器
	APM.BusinessDetailExperienceBrowserRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_experience_browser_ctrl",
			"./views/detail_experience_browser_view"
		],
		setupController: function(ctrl, params, transition) {
			// if(!ctrl.get("cancelSet")){
			// 	overviewOrCenterSwitch = "system";
			// }
			if (overviewOrCenterSwitch == "business") {
				ctrl.set("panelTitle", "交易量");
			} else {
				ctrl.set("panelTitle", "吞吐量");
			}
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("cancelSet", false);
		}
	});

	//最终用户体验-页面
	APM.BusinessDetailExperiencePageRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_experience_page_ctrl",
			"./views/detail_experience_page_view"
		],
		setupController: function(ctrl, params, transition) {
			if (overviewOrCenterSwitch == "business") {
				ctrl.set("defaultSelect", "business/response_time/avg");
			} else {
				ctrl.set("defaultSelect", "system/response_time/avg");
			}
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("cancelSet", false);
		}
	});

	//最终用户体验-位置
	APM.BusinessDetailExperienceAreaRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_experience_area_ctrl",
			"./views/detail_experience_area_view"
		],
		setupController: function(ctrl, params, transition) {
			if (overviewOrCenterSwitch == "business") {
				ctrl.set("defaultSelect", "business/response_time/avg");
			} else {
				ctrl.set("defaultSelect", "system/response_time/avg");
			}
			heightScroll();
		},
		deactivate: function() {
			this.controller.set("cancelSet", false);
		}
	});

	//节点详情
	APM.BusinessDetailNodedetailRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_ctrl",
			"./views/detail_nodedetail_view"
		],
		model: function(params, transition) {
			var topoNodes = transition.resolvedModels["business.detail"].topoNodes;
			var node = topoNodes.findBy("id", params.node_id);
			if (node) {
				return node;
			} else {
				transition.abort();
				this.transitionTo("business.detail.gauge");
			}
		},
		setupController: function(ctrl, params, transition) {
			ctrl.set("model", params);
			if($.cookie("isThreshold") == null){
				ctrl.transitionToRoute("business.detail.nodedetail.overview");
			};
			if (overviewOrCenterSwitch == "system") {
				ctrl.set("listHidden", true);
			} else {
				ctrl.set("listHidden", false);
			};
		}
	});

	APM.BusinessDetailNodedetailIndexRoute = Business.Route.extend({
		redirect: function() {
			this.transitionTo("business.detail.nodedetail.overview");
		}
	});

	//节点详情-服务概况
	APM.BusinessDetailNodedetailOverviewRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_overview_ctrl",
			"./views/detail_nodedetail_overview_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.setup(params);
			ctrl.set("model", params);
		}
	});

	//节点详情-动态菜单
	APM.BusinessDetailNodedetailDynamicRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_dynamic_ctrl",
			"./views/detail_nodedetail_dynamic_view"
		],
		model: function(params, transition) {
			return this.store.ajax("GET", "node/menus", {
					nodeId: transition.resolvedModels["business.detail.nodedetail"].id
				})
				.then(function(response) {
					var menu = Ember.A(response.result).findBy("id", parseInt(params.menu_id));
					if (menu) {
						return menu;
					} else {
						transition.abort();
						this.transitionTo("business.detail.nodedetail.overview");
					}
				});
		},
		setupController: function(ctrl, params, transition) {
			ctrl.set("model", params);
			ctrl.keyUpEnent();
			ctrl.initTime();
			ctrl.requireDataList1();
		}
	});

	//节点详情-错误码
	APM.BusinessDetailNodedetailErrorcodeRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_errorcode_ctrl",
			"./views/detail_nodedetail_errorcode_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.set("model", params);
			ctrl.keyUpEnent();
			
			ctrl.initTime();
			ctrl.requireData();

			heightScroll();
		},
		deactivate: function() {
			this.controller.set('keyWord', '');
			this.controller.set("model", null);
			this.controller.set("pageIndex", 0);
		}
	});

	//节点详情-最新告警
	APM.BusinessDetailNodedetailAlarmsRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_alarms_ctrl",
			"./views/detail_nodedetail_alarms_view"
		],
		setupController: function(ctrl, params, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", params);
			ctrl.initTime();
			ctrl.requireAlerts();
		}
	});

	//节点详情-交易跟踪
	APM.BusinessDetailNodedetailTrackRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_track_ctrl",
			"./views/detail_nodedetail_track_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", model);
			ctrl.set('channelValue', '');
			ctrl.set('typeValue', '');
			ctrl.set('serviceValue', '');
			ctrl.initTime();
			ctrl.requireAlerts();
			ctrl.selectData();
		},
		deactivate: function() {
			this.controller.set('keyWord', '');
			this.controller.offEvent();
		},
	});

	//节点详情-错误交易
	APM.BusinessDetailNodedetailWrongtransactionRoute = Business.Route.extend({

		dependences: [
			"./ctrls/detail_nodedetail_wrongtransaction_ctrl",
			"./views/detail_nodedetail_wrongtransaction_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", model);
			ctrl.set('channelValue', '');
			ctrl.set('typeValue', '');
			ctrl.initTime();
			ctrl.requireAlerts();
			ctrl.selectData();
		},
		deactivate: function() {
			this.controller.set('keyWord', '');
			this.controller.offEvent();
		},
	});

	//节点详情-慢交易
	APM.BusinessDetailNodedetailSlowtradeRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_slowtrade_ctrl",
			"./views/detail_nodedetail_slowtrade_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.keyUpEnent();
			ctrl.set("model", model);
			ctrl.set('channelValue', '');
			ctrl.set('typeValue', '');
			ctrl.initTime();
			ctrl.requireAlerts();
			ctrl.selectData();
		},
		deactivate: function() {
			this.controller.set('keyWord', '');
			this.controller.offEvent();
		}
	});


	//节点详情-集群节点
	APM.BusinessDetailNodedetailClusterRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_nodedetail_cluster_ctrl",
			"./views/detail_nodedetail_cluster_view"
		],
		setupController: function(ctrl, model, transition) {
			ctrl.set("model", model);
			ctrl.initTime();
			ctrl.requireData();
		}
	});

	//应用配置管理
	APM.BusinessDetailConfigRoute = Business.Route.extend({
		dependences: [
			"./ctrls/detail_config_ctrl",
			"./views/detail_config_view"
		],
		model: function(params, transition) {
			var route = this;
			var appId = transition.resolvedModels["business.detail"].id
			return this.store.ajax("GET", "applications/" + appId, {
					"panelId": overviewOrCenterSwitch
				})
				.then(function(response) {
					return response.application;
				})
		},
		setupController: function(ctrl, model, transition) {
			ctrl.resetForm();
			ctrl.requireThresholds();
			if (!ctrl.get("cancelSet")) {
				if (overviewOrCenterSwitch == "business") {
					ctrl.set("isShowSystemOrBusiness", false);
				} else {
					ctrl.set("isShowSystemOrBusiness", true);
				}
			}
			setTimeout(function() {
				ctrl.defaultAlarmTime(model.validTimeMap);
			}, 10)

		},
		deactivate: function() {
			this.controller.reset();
		}
	});
})();